//
//  UIWebViewController.m
//  freeLance
//
//  Created by s.zalozniy on 4/13/13.
//  Copyright (c) 2013 Mozi Development. All rights reserved.
//

#import "SVProgressHUD.h"

#import "UIWebViewController.h"


@interface UIWebViewController ()

@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, assign) BOOL isLoading;

-(void) backButtonTapped:(id)sender;

@end


@implementation UIWebViewController

@synthesize webView;

#pragma mark - Instance Initialization

-(id) initWithStremUrl:(NSURL *)streamUrl {
    self = [super initWithNibName:nil bundle:nil];
    if (!self) {
        return nil;
    }
    
    self.isLoading = NO;
    
    webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    webView.scalesPageToFit = YES;
    webView.delegate = self;
    [self.webView loadRequest:[NSURLRequest requestWithURL:streamUrl]];
    [self.view addSubview:self.webView];
    
	
	if (IS_IPAD) {
		UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", nil)
																		   style:UIBarButtonItemStyleBordered
																		  target:self
																		  action:@selector(backButtonTapped:)];
		
		UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
										   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
										   target:nil action:nil];
		negativeSpacer.width = 30;
		self.navigationItem.leftBarButtonItems = [NSArray
												  arrayWithObjects:negativeSpacer, backButtonItem, nil];
	}
    
    return self;
}


#pragma mark - Memory managment

-(void) dealloc {
    webView.delegate = nil;
    [NSObject cancelPreviousPerformRequestsWithTarget:self
                                             selector:@selector(hideHood)
                                               object:nil];
    [webView removeFromSuperview];
    
    [SVProgressHUD dismiss];
}


-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (self.isLoading) {
        [SVProgressHUD showWithStatus:NSLocalizedString(@"Loading...", nil) maskType:SVProgressHUDMaskTypeNone];
    }
}

#pragma mark - Private methods

-(void) hideHood {
    [SVProgressHUD dismiss];
}

#pragma mark - Actioin methods

-(void) backButtonTapped:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
    [SVProgressHUD dismiss];
}

#pragma mark - Delegated methods

-(void) webViewDidStartLoad:(UIWebView *)webView {
    self.isLoading = YES;
}


-(void) webViewDidFinishLoad:(UIWebView *)webView {
    self.isLoading = NO;
    [NSObject cancelPreviousPerformRequestsWithTarget:self
                                             selector:@selector(hideHood)
                                               object:nil];
    
    [self performSelector:@selector(hideHood)
               withObject:nil
               afterDelay:1.0f];
}


-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    self.isLoading = NO;
    [SVProgressHUD showErrorWithStatus:NSLocalizedString(@"Loading fialed", nil)];
}


@end
